package com.chuji.crmcj.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chuji.crmcj.R;
import com.chuji.crmcj.bean.SalerInfo;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.utils.UrlUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by w.pitt on 2016/2/17.
 */
public class NoUserAdapter extends BaseAdapter {
    List<String> mData = new ArrayList<String>(Arrays.asList("去洗手间", "送快递、外卖", "非购车业务"));
    private ViewHolder holder;
    List<String> mDefReason = new ArrayList<String>(Arrays.asList("选购竞品", "放弃购车", "同城购车"));

    List<String> mCallItem = new ArrayList<String>(Arrays.asList("商务电话回访", "邀约来店沟通交流", "邀约试驾", "邀约参加优惠活动", "其他"));

    List<String> mFolTypes = new ArrayList<String>(Arrays.asList("微信", "QQ", "上门", "其他"));

    List<String> mBackCall = new ArrayList<String>(Arrays.asList("客户关怀", "活动通知", "其他"));

    List<String> mLevels = new ArrayList<String>(Arrays.asList("A级", "B级", "C级", "N级", "H级"));
    private int lable = -1;

    public NoUserAdapter(int i) {
        this.lable = i;
        /**
         * 0   mdatas
         * 1   mDefReason
         * 2   mCallItem
         * 3   mFolType
         * 4   mBackCall
         * 5   mLevels
         */
    }

    public NoUserAdapter() {

    }

    private SalerInfo mSalerInfo;

    public NoUserAdapter(int lable, SalerInfo salerInfo) {
        this.mSalerInfo = salerInfo;
        this.lable = lable;
    }

    @Override
    public int getCount() {
        if (lable == 0) {
            return mData.size();
        } else if (lable == 1) {
            return mDefReason.size();
        } else if (lable == 2) {
            return mCallItem.size();
        } else if (lable == 3) {
            return mFolTypes.size();
        } else if (lable == 4) {
            return mBackCall.size();
        } else if (lable == 5) {
            return mLevels.size();
        } else if (lable == 6) {
            return mSalerInfo.getApiParamObj().size()==0?0:mSalerInfo.getApiParamObj().size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private int selected = -1;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = View.inflate(UIUtils.getContext(), R.layout.dialog_nouser_reason, null);
            holder = new ViewHolder();
            holder.mRlClick = (RelativeLayout) convertView.findViewById(R.id.rl_click);
            holder.mIvClick = (ImageView) convertView.findViewById(R.id.clickshow);
            holder.mIv_icon = (ImageView) convertView.findViewById(R.id.iv_icon);
            holder.mTvReason = (TextView) convertView.findViewById(R.id.tv_reason);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (lable == 0) {
            holder.mTvReason.setText(mData.get(position));
        } else if (lable == 1) {
            holder.mTvReason.setText(mDefReason.get(position));
        } else if (lable == 2) {
            holder.mTvReason.setText(mCallItem.get(position));
        } else if (lable == 3) {
            holder.mTvReason.setText(mFolTypes.get(position));
        } else if (lable == 4) {
            holder.mTvReason.setText(mBackCall.get(position));
        } else if (lable == 5) {
            holder.mTvReason.setText(mLevels.get(position));
        } else if (lable == 6) {
            Glide.with(UIUtils.getContext()).load(UrlUtils.BASEIP + mSalerInfo.getApiParamObj().get(position).getAvatar()).into(holder.mIv_icon);
            if (mSalerInfo.getApiParamObj().get(position).getOriginal() == 1) {
                //原销售
                holder.mTvReason.setText(mSalerInfo.getApiParamObj().get(position).getRealName() + "(原负责人)");
            } else {
                holder.mTvReason.setText(mSalerInfo.getApiParamObj().get(position).getRealName());
            }
        }

        holder.mRlClick.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selected = position;
                NoUserAdapter.this.notifyDataSetChanged();
            }
        });

        if (position == 0 && selected == -1) {
            holder.mIvClick.setImageDrawable(UIUtils.getDrawable(R.drawable.checked));
            saveSelect(lable, position);
        } else {
            if (position == selected) {
                holder.mIvClick.setImageDrawable(UIUtils.getDrawable(R.drawable.checked));
                saveSelect(lable, position);
            } else {
                holder.mIvClick.setImageDrawable(UIUtils.getDrawable(R.drawable.unchecked));
            }
        }
        return convertView;
    }

    String invalid = "";
    String defReason = "";
    String callAim = "";
    String newFol = "";
    String backCall = "";
    String currentLevel = "";
    String transCus = "";
    String selectItem = "";
    String selectValue = "";

    private void saveSelect(int lable, int position) {
        if (lable == 0) {
                /*无效客户原因*/
            invalid = mData.get(position);
//                holder.mTvReason.setText(invalid);
            selectItem = "invalid";
            selectValue = invalid;
//            SPUtils.saveString(UIUtils.getContext(), "invalid", invalid);
        } else if (lable == 1) {
                /*战败原因*/
            defReason = mDefReason.get(position);
//                holder.mTvReason.setText(defReason);

            selectItem = "defReason";
            selectValue = defReason;
//            SPUtils.saveString(UIUtils.getContext(), "defReason", defReason);
        } else if (lable == 2) {
                /*客户详情中 电话目的*/
            callAim = mCallItem.get(position);
//                holder.mTvReason.setText(position);
            if (position == 1 || position == 2 || position == 3) {
                //只有选择了 123 的 存入 1 。确定弹出
                SPUtils.saveInt(UIUtils.getContext(), "isShowCal", 1);
            } else {
                SPUtils.saveInt(UIUtils.getContext(), "isShowCal", 0);
            }
            selectItem = "callAim";
            selectValue = callAim;
//            SPUtils.saveString(UIUtils.getContext(), "callAim", callAim);
        } else if (lable == 3) {
                /*添加新的跟进*/
            newFol = mFolTypes.get(position);
//                holder.mTvReason.setText(newFol);
            selectItem = "newFol";
            selectValue = newFol;
//            SPUtils.saveString(UIUtils.getContext(), "newFol", newFol);
        } else if (lable == 4) {
                /*保有客户  电话回访*/
            backCall = mBackCall.get(position);
            selectItem = "backCall";
            selectValue = backCall;
//                holder.mTvReason.setText(backCall);
//            SPUtils.saveString(UIUtils.getContext(), "backCall", backCall);
        } else if (lable == 5) {
                /*修改等级*/
            currentLevel = mLevels.get(position);

            selectItem = "currentLevel";
            selectValue = currentLevel;
//                holder.mTvReason.setText(currentLevel);
//            SPUtils.saveString(UIUtils.getContext(), "currentLevel", currentLevel);
        } else if (lable == 6) {
                /*转移客户*/
            transCus = mSalerInfo.getApiParamObj().get(position).getSalesID();
            selectItem = "transCus";
            selectValue = transCus;
//                holder.mTvReason.setText(currentLevel);
//            SPUtils.saveString(UIUtils.getContext(), "transCus", transCus);
        }
        SPUtils.saveString(UIUtils.getContext(), selectItem, selectValue);
    }

    static class ViewHolder {
        public RelativeLayout mRlClick;
        public ImageView mIvClick;
        public TextView mTvReason;
        public ImageView mIv_icon;
    }
}
